import uuid

from django.core.validators import MaxValueValidator
from django.db import models
from django.db.models import SET_NULL
from django.utils import timezone

from apps.ExamManagement.models import Exam
from apps.QuestionManagement.models import Questions
from apps.UserManagement.models import User


class Results(models.Model):
    class Meta:
        db_table = 'result'

    objects = models.Manager()
    id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255)
    # 绑定考试ID
    exam = models.ForeignKey(Exam, null=True, max_length=255, on_delete=models.CASCADE, help_text='绑定考试ID')
    # 绑定用户ID
    user = models.ForeignKey(User, null=True, max_length=255, on_delete=SET_NULL, help_text='绑定用户ID')
    # 总分
    total_score = models.IntegerField(null=True, blank=True, verbose_name='总分', default=0)
    # 创建时间
    created_at = models.DateTimeField(default=timezone.now, help_text='创建时间')
    # 更新时间
    updated_at = models.DateTimeField(auto_now=True, null=True, help_text='更新时间')


class ResultQuestionSolution(models.Model):
    """答题卡,试题,作答,得分 关联表"""
    objects = models.Manager()
    results = models.ForeignKey(Results,on_delete=models.CASCADE,blank=True)
    question = models.ForeignKey(Questions, on_delete=models.CASCADE,blank=True)
    solution = models.TextField(null=True, blank=True, verbose_name='答题内容')
    score = models.IntegerField(null=True, blank=True, verbose_name='得分', default=0)
    # 创建时间
    created_at = models.DateTimeField(default=timezone.now, help_text='创建时间')
    # 更新时间
    updated_at = models.DateTimeField(auto_now=True, null=True, help_text='更新时间')

    class Meta:
        db_table = 'results_question'
        unique_together = ('results', 'question', 'score')
